namespace DukeyTbker.API {
    using System;
    using System.IO;
    using System.Net;
    using System.Text;
    using System.Text.RegularExpressions;

    public class Comm {
        public static string ContainerCookies = "";
        public static string Refer = "";

        public static Encoding GetPageEncoding(string string_0) {
            try {
                Encoding encoding;
                string str3;
                Regex regex;
                WebResponse response = WebRequest.Create(string_0).GetResponse();
                string name = null;
                string str2 = response.Headers["content-type"];
                if (str2 != null) {
                    str2 = str2.ToLower();
                    if (str2.IndexOf("charset") > 0) {
                        name = str2.Substring(str2.IndexOf("=") + 1);
                    }
                }
                if (name == null) {
                    StreamReader reader = new StreamReader(response.GetResponseStream());
                    regex = new Regex("<meta.*? charset=(['\"]?).*?(?<code>[^\\s]+)\\1.*?>", RegexOptions.IgnoreCase);
                    while ((str3 = reader.ReadLine()) != null) {
                        if (regex.IsMatch(str3)) {
                            goto Label_009A;
                        }
                    }
                }
                goto Label_00CD;
            Label_009A:
                name = regex.Match(str3).Groups["code"].Value.Replace("\"", "");
            Label_00CD:
                encoding = Encoding.ASCII;
                if (name != null) {
                    try {
                        encoding = Encoding.GetEncoding(name);
                    }
                    catch {
                        encoding = Encoding.GetEncoding("gb2312");
                    }
                }
                try {
                    response.Close();
                }
                catch (Exception) {
                }
                return encoding;
            }
            catch {
                return GetPageEncodingOfAgent(string_0);
            }
        }

        public static Encoding GetPageEncodingOfAgent(string string_0) {
            try {
                Regex regex;
                Encoding aSCII;
                string str3;
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(string_0);
                request.Credentials = CredentialCache.DefaultCredentials;
                request.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.75 Safari/535.7";
                WebResponse response = request.GetResponse();
                string name = null;
                string str2 = response.Headers["content-type"];
                if (str2 != null) {
                    str2 = str2.ToLower();
                    if (str2.IndexOf("charset") > 0) {
                        name = str2.Substring(str2.IndexOf("=") + 1);
                    }
                }
                if (name == null) {
                    StreamReader reader = new StreamReader(response.GetResponseStream());
                    regex = new Regex("<meta.*? content=(['\"]?).*?charset=(?<code>[^\\s]+)\\1.*?>", RegexOptions.IgnoreCase);
                    while ((str3 = reader.ReadLine()) != null) {
                        if (regex.IsMatch(str3)) {
                            goto Label_00B5;
                        }
                    }
                }
                goto Label_00D7;
            Label_00B5:
                name = regex.Match(str3).Groups["code"].Value;
            Label_00D7:
                aSCII = Encoding.ASCII;
                if (name != null) {
                    try {
                        aSCII = Encoding.GetEncoding(name);
                    }
                    catch {
                        aSCII = Encoding.ASCII;
                    }
                }
                try {
                    response.Close();
                }
                catch (Exception) {
                }
                return aSCII;
            }
            catch {
            }
            return Encoding.GetEncoding("gb2312");
        }

        public static string Html(bool isagent, string strLink, Encoding encoding) {
            string str;
            if (isagent) {
                return HtmlofAgent(strLink, encoding);
            }
            if (encoding == null) {
                encoding = GetPageEncoding(strLink);
            }
            try {
                HttpWebRequest request = WebRequest.Create(strLink) as HttpWebRequest;
                request.Headers["Accept-Encoding"] = "gzip,deflate";
                request.AutomaticDecompression = DecompressionMethods.GZip;
                HttpWebResponse response = request.GetResponse() as HttpWebResponse;
                using (Stream stream = response.GetResponseStream()) {
                    using (StreamReader reader = new StreamReader(stream, encoding)) {
                        str = reader.ReadToEnd();
                        reader.Close();
                        stream.Close();
                        response.Close();
                    }
                }
                if (str.Trim() == "") {
                    return HtmlofAgent(strLink, encoding);
                }
            }
            catch {
                return HtmlofAgent(strLink, encoding);
            }
            return str;
        }

        public static string HtmlofAgent(string strLink, Encoding encoding) {
            string str;
            if (string.IsNullOrEmpty(strLink)) {
                return "";
            }
            if (encoding == null) {
                encoding = GetPageEncoding(strLink);
            }
            try {
                HttpWebRequest request = WebRequest.Create(strLink) as HttpWebRequest;
                request.Headers["Accept-Encoding"] = "gzip,deflate";
                request.AutomaticDecompression = DecompressionMethods.GZip;
                request.Credentials = CredentialCache.DefaultCredentials;
                request.Referer = Refer;
                request.KeepAlive = true;
                request.AllowAutoRedirect = false;
                if (!string.IsNullOrEmpty(ContainerCookies)) {
                    CookieContainer container = new CookieContainer();
                    foreach (string str2 in ContainerCookies.Split(new char[] { ';' })) {
                        string name = str2.Split(new char[] { '=' })[0].Trim();
                        string str4 = str2.Replace(name + "=", "").Replace(",", "%2").Trim();
                        container.Add(new Uri(Refer), new Cookie(name, str4));
                    }
                    request.CookieContainer = container;
                }
                request.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.75 Safari/535.7";
                HttpWebResponse response = request.GetResponse() as HttpWebResponse;
                using (Stream stream = response.GetResponseStream()) {
                    using (StreamReader reader = new StreamReader(stream, encoding)) {
                        str = reader.ReadToEnd();
                        reader.Close();
                        stream.Close();
                        response.Close();
                    }
                    return str;
                }
            }
            catch (Exception) {
                str = "";
            }
            return str;
        }
    }
}

